package com.villaday.idf.activity;

import androidx.appcompat.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.R;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.custom.LoadingDialog;
import com.villaday.idf.model.defaultBroken;
import com.villaday.idf.model.feedbackEntity;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.PreferencesUtil;
import com.villaday.idf.util.ToastUtil;
import com.villaday.idf.util.ToolBarUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 添加分组
 */
public class DefaultRatioActivity extends BaseActivity implements OkHttpUtil.OnDataListener {

    @BindView(R.id.toolbar_text)
    public Toolbar toolbar;

    @BindView(R.id.et_rabit)
    public EditText etInputBalance;
    @BindView(R.id.tv_save)
    public TextView tvSave;


    private Gson gson = new GsonBuilder().create();
    private String userId;
    private LoadingDialog loadingDialog;


    @Override
    protected int getViewResId() {
        return R.layout.activity_default_ratio;
    }

    @Override
    protected void init() {
        userId = PreferencesUtil.getUserId(this);
        ToolBarUtil.initToolBar(toolbar, "默认分佣比例", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });

        String defaultRabit = getIntent().getStringExtra("rabit");
        if (!TextUtils.isEmpty(defaultRabit))
            etInputBalance.setText(defaultRabit);
    }


    @Override
    protected void loadData() {

    }


    private void getData(String rabit) {
        loadingDialog = LoadingDialog.newInstance("提交中...");
        loadingDialog.show(getFragmentManager());
        Map<String, Object> map = new HashMap<>();
        OkHttpUtil.postJson(this, Constant.URL.default_broken, new defaultBroken(rabit), map, this);
    }


    @Override
    public void onResponse(String url, String json) {
        if (!TextUtils.isEmpty(json)) {
            if (Constant.URL.default_broken.equals(url)) { //添加组成员
                dismissLoading();
                LogeUtil.e("default_broken", json);
                feedbackEntity fbe = gson.fromJson(json, feedbackEntity.class);
                if (Constant.Strings.SUC.equals(fbe.getCode())) {
                    ToastUtil.initToast(this, "操作成功");
                    setResult(RESULT_OK);
                    finish();
                }
            }
        }
    }


    @Override
    public void onFailure(String url, String error) {

    }

    private void dismissLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }


    @OnClick({R.id.tv_save})
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.tv_save: {
                if (TextUtils.isEmpty(etInputBalance.getText().toString())) {
                    ToastUtil.initToast(this, "请输入分佣比例");
                } else if (Double.parseDouble(etInputBalance.getText().toString().toString()) <= 0) {
                    ToastUtil.initToast(this, "输入数据错误");
                } else {
                    getData(etInputBalance.getText().toString().toString());
                }
                break;
            }
        }

    }


}
